/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * aBMEntidadPnl.java
 *
 * Created on 26-dic-2011, 18:50:20
 */
package util.components;

import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JTable;
import util.GestorComponenteVista;

/**
 *
 * @author Raul
 */
public class TablaABMPnl extends javax.swing.JPanel {
    
    private IGestorABM gestorVista;

    public TablaABMPnl() {
        initComponents();
    }
    
    /** Creates new form aBMEntidadPnl */
    public TablaABMPnl(IGestorABM gestorVista) {
        initComponents();
        this.gestorVista = gestorVista;        
    }

    public IGestorABM getGestorVista() {
        return gestorVista;
    }

    public void setGestorVista(IGestorABM gestorVista) {
        this.gestorVista = gestorVista;
    }
    public JButton getEliminarBtn() {
        return eliminarBtn;
    }

    public JButton getImprimirBtn() {
        return imprimirBtn;
    }

    public JButton getModificarEntidadBtn() {
        return modificarEntidadBtn;
    }

    public JButton getNuevaEntidadBtn() {
        return nuevaEntidadBtn;
    }

    public JTable getTabla() {
        return tabla;
    }
    
    public void actualizarTabla(){
        List datos = GestorComponenteVista.getObjetosEnTabla(tabla);
        getGestorVista().cargarModeloComponente();
        getGestorVista().cargarDatos(datos);
    }
    
    public void setImpresionHabilitada(boolean b){
        imprimirBtn.setVisible(b);
    }
    
    public boolean getImpresionHabilitada() {
        return imprimirBtn.isVisible();
    }
        
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popupMenu = new javax.swing.JPopupMenu();
        modificarItem = new javax.swing.JMenuItem();
        eliminarItem = new javax.swing.JMenuItem();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        eliminarBtn = new javax.swing.JButton();
        modificarEntidadBtn = new javax.swing.JButton();
        nuevaEntidadBtn = new javax.swing.JButton();
        imprimirBtn = new javax.swing.JButton();

        popupMenu.setInvoker(tabla);

        modificarItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/pencil.png"))); // NOI18N
        modificarItem.setText("Modificar");
        modificarItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modificarEntidadBtnActionPerformed(evt);
            }
        });
        popupMenu.add(modificarItem);

        eliminarItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/delete.png"))); // NOI18N
        eliminarItem.setText("Eliminar");
        eliminarItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarBtnActionPerformed(evt);
            }
        });
        popupMenu.add(eliminarItem);

        setBorder(new util.components.RoundedBorder());

        tabla.setAutoCreateRowSorter(true);
        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tabla.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabla);

        eliminarBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/delete.png"))); // NOI18N
        eliminarBtn.setText("Eliminar");
        eliminarBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarBtnActionPerformed(evt);
            }
        });

        modificarEntidadBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/pencil.png"))); // NOI18N
        modificarEntidadBtn.setText("Modificar");
        modificarEntidadBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modificarEntidadBtnActionPerformed(evt);
            }
        });

        nuevaEntidadBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/add.png"))); // NOI18N
        nuevaEntidadBtn.setText("Nuevo");
        nuevaEntidadBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nuevaEntidadBtnActionPerformed(evt);
            }
        });

        imprimirBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/printer.png"))); // NOI18N
        imprimirBtn.setText("Imprimir");
        imprimirBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                imprimirBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(imprimirBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(nuevaEntidadBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(modificarEntidadBtn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(eliminarBtn)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(nuevaEntidadBtn)
                    .addComponent(modificarEntidadBtn)
                    .addComponent(eliminarBtn)
                    .addComponent(imprimirBtn))
                .addGap(6, 6, 6))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void nuevaEntidadBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nuevaEntidadBtnActionPerformed
        try {
            this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            getGestorVista().registrarNuevaEntidad();
        } finally {
            this.setCursor(Cursor.getDefaultCursor());
        }
    }//GEN-LAST:event_nuevaEntidadBtnActionPerformed

    private void modificarEntidadBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modificarEntidadBtnActionPerformed
        if (tabla.getSelectedRow() != -1) {
            try {
                this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                getGestorVista().modificar(GestorComponenteVista.getObjetoSeleccionado(tabla));
            } finally {
                this.setCursor(Cursor.getDefaultCursor());
            }
        }  
    }//GEN-LAST:event_modificarEntidadBtnActionPerformed

    private void eliminarBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarBtnActionPerformed
        if (tabla.getSelectedRow() != -1 && 0 == gestorVista.mostrarDialogoDeEliminacion()) {
            getGestorVista().eliminar(GestorComponenteVista.getObjetoSeleccionado(tabla));
            GestorComponenteVista.eliminarFilaTabla(tabla, tabla.getSelectedRow());
        }
    }//GEN-LAST:event_eliminarBtnActionPerformed

    private void tablaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseClicked
        if (evt.getButton() == 3) {
            for (MouseListener ml : tabla.getMouseListeners()) {
                ml.mousePressed(new MouseEvent(tabla, MouseEvent.MOUSE_PRESSED, 1,
                        0, evt.getPoint().x, evt.getPoint().y, 1, true, MouseEvent.BUTTON1));
                ml.mouseReleased(new MouseEvent(tabla, MouseEvent.MOUSE_RELEASED, 1,
                        0, evt.getPoint().x, evt.getPoint().y, 1, true, MouseEvent.BUTTON1));
            }
            if (tabla.getSelectedRow() != -1 ) popupMenu.show(tabla, evt.getPoint().x, evt.getPoint().y);
        }
        if (evt.getClickCount()== 2) modificarEntidadBtnActionPerformed(null);
    }//GEN-LAST:event_tablaMouseClicked

    private void imprimirBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_imprimirBtnActionPerformed
        if (tabla.getModel().getRowCount()!= 0) 
            gestorVista.imprimir(GestorComponenteVista.getObjetosEnTabla(tabla));
    }//GEN-LAST:event_imprimirBtnActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton eliminarBtn;
    private javax.swing.JMenuItem eliminarItem;
    private javax.swing.JButton imprimirBtn;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton modificarEntidadBtn;
    private javax.swing.JMenuItem modificarItem;
    private javax.swing.JButton nuevaEntidadBtn;
    private javax.swing.JPopupMenu popupMenu;
    private javax.swing.JTable tabla;
    // End of variables declaration//GEN-END:variables


}
